package com.invengo.testrfid.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.invengo.testrfid.R;
import com.invengo.testrfid.app.utils.MyUIUtils;
import com.invengo.testrfid.di.component.DaggerUploadDetailComponent;
import com.invengo.testrfid.di.module.UploadDetailModule;
import com.invengo.testrfid.mvp.contract.UploadDetailContract;
import com.invengo.testrfid.mvp.presenter.UploadDetailPresenter;
import com.invengo.testrfid.mvp.ui.adapter.MyDefaultAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CheckoutDataActivity extends BaseActivity<UploadDetailPresenter> implements UploadDetailContract.View {

    @BindView(R.id.lv_type)
    RecyclerView recyclerView;

    @BindView(R.id.sw_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    private LinearLayoutManager mLayoutManager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUploadDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .uploadDetailModule(new UploadDetailModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_upload_detail; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        mPresenter.initView(this);
        setTitle("数据导出");
    }

    @Override
    public void showLoading() {

        MyUIUtils.showProgressDia(this);
    }

    @Override
    public void hideLoading() {

        MyUIUtils.hideProgressDia(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void setAdapter(MyDefaultAdapter adapter) {

        swipeRefreshLayout.setEnabled(false);
        mLayoutManager = new LinearLayoutManager(this);
        ArmsUtils.configRecycleView(recyclerView, mLayoutManager);
        recyclerView.setAdapter(adapter);
    }

    @OnClick(R.id.btn_upload)
    public void uploadData(View view) {

        mPresenter.exportData();
    }

    private void setTitle(String title) {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(title);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        mPresenter.showSearchDia();
        return true;
    }
}
